import log4js from 'log4js'
// import path from 'path'

// const logConfigFilePath = path.resolve(__dirname, 'log4js.json')
// console.log(`日志配置文件路径:${logConfigFilePath}`)
log4js.configure({
  appenders: {
    console: {
      type: 'console',
      level: 'DEBUG',
      layout: {
        type: 'pattern',
        pattern: '%d{yyyy/MM/dd hh:mm:ss} %p > %m'
      }
    },
    file: {
      type: 'file',
      filename: 'logs/app.log',
      level: 'INFO',
      encoding: 'utf-8',
      pattern: 'yyyy-MM-dd',
      layout: {
        type: 'pattern',
        pattern: '%d{yyyy/MM/dd hh:mm:ss} %p > %m'
      },
      keepFileExt: true,
      maxLogSize: 10485760,
      backups: 3,
      compress: true,
      alwaysIncludePattern: true
    }
  },
  categories: {
    default: { appenders: ['console'], level: 'debug' },
    infoLogFile: { appenders: ['file'], level: 'info' }
  }
})

/**
 * 获取日志记录工具实例
 * @example
 * const logger = loggerFactory('user')
 * logger.debug('你好') // 输出: [2010-01-17 11:43:37.987] [DEBUG] user - 你好
 * logger.info('你好') // 输出: [2010-01-17 11:43:37.987] [INFO] user - 你好
 * logger.warn('你好') // 输出: [2010-01-17 11:43:37.987] [WARN] user - 你好
 * logger.error('你好') // 输出: [2010-01-17 11:43:37.987] [ERROR] user - 你好
 * logger.fatal('你好') // 输出: [2010-01-17 11:43:37.987] [FATAL] user - 你好
 * @param category 日志分类标识
 */
export function loggerFactory(category: string) {
  return log4js.getLogger(category)
}
